#PyMinecraft文字转图片模块
import PIL.Image as Image
import random
#图片文字互转
class Image_:
    def Bin_Image(bin,Image_File_Name,x=7):
        y = len(bin)
        Img = Image.new('RGBA', (x, y))
        for i in range(0, x):
            for j in range(0, y):
                try:
                    if bin[j][i] == "0":
                        Img.putpixel([i, j], (0, 0, 0))
                    elif bin[j][i] == "1":
                        Img.putpixel([i, j], (255, 255, 255))
                except:
                    Img.putpixel([i, j], (255, 0, 0))
        Img.save(Image_File_Name)

    def Image_Str(Image_File_Name):
        Img = Image.open(Image_File_Name)
        Bin = []
        x = Img.size[0]
        y = Img.size[1]
        for j in range(0, y):
            Tmp = ""
            for i in range(0, x):
                if Img.getpixel((i, j)) == (255, 255, 255, 255):
                    Tmp += "1"
                elif Img.getpixel((i, j)) == (0, 0, 0, 255):
                    Tmp += "0"
            Bin.append(Tmp)
        return Str.Bin_Str(Bin)
#二进制文字互转
class Str:
    def Str_Bin(str):
        retur = []
        for i in str:
            retur.append(bin(ord(i)).replace("0b",""))
        return retur
    def Bin_Str(bin):
        retur = ""
        for i in bin:
            retur += chr(int(i,2))
        return retur
#区块生成系统
class chunk:
    '''
    savename(str)是存档的名字,比如：新的世界1
    generate(list)是世界生成器的参数,需要给予的数据有：会随机生成的方块，草方块最高层数，草方块最低层数，基岩层数,如[["grass_block","stone","iron_ore","gold_ore","grass"],60,57,0]
    就可以生成一个带有：草方块、石头、铁矿石、金矿石、草，的世界，最高的草的高度为3b，最高的草方块层数为60，最低的草方块层数为57，基岩在第0层的世界,生成器中方块列表无需含有air和bedrock和grass和grass_blcok
    '''
    def __init__(self,savename,generate):
        self.savename = savename
        self.generate = generate
    def create(self):
        textdict = ""
        i = 0
        Maxblocky = self.generate[1]
        Mingrassy = self.generate[2]
        Minblocky = self.generate[3]
        for x in range(0,16):
            for z in range(0,16):
                for y in range(0,256):
                    if y == Minblocky:
                        block = "bedrock"
                    elif y >= Mingrassy and y<=Maxblocky:
                        block = random.sample(['grass','air'],1)[0]
                    elif y <= Maxblocky:
                        block = random.sample(self.generate[0], 1)[0]
                    else:
                        block = "air"
                    textdict += f"x:{x} y:{y} z:{z},{block};"
        Image_.Bin_Image(Str.Str_Bin(textdict),f'{self.savename}.png')
def readchunk(x,y,z,image):
    Text = Image_.Image_Str(image)
    for i in Text.split(";"):
        split = i.split(",")
        if split[0] == f"x:{x} y:{y} z:{z}":
            print(split[1])